﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace benefit.Controllers
{
    public class CompanyHistoryController : BenefitHelper.Tools.BaseControl
    {
        //
        // GET: /CompanyHistory/

        public ActionResult Index(int uid, string pdate)
        {
            string js = "";
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            ViewBag.datequery = cookie.GetCookie("querydate");
            int operId = Convert.ToInt32(cookie.GetCookie("LoginUserId"));

            if (BenefitHelper.Sys.Company.HasInited(db))
            {
                ViewBag.company = db.Company.First();
            }
            else
            {
                ViewBag.company = new BenefitHelper.Sys.Company();
            }
            if (BenefitHelper.Sys.VisitPrower.HasCompanyPrower(operId, db))
            {
                if (pdate.Length > 4)//查询月数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = pdate.Substring(4, 2);
                    //  ViewBag.queryday = pdate.Substring(6, 2);
                    ViewBag.pdate = pdate;
                    int year = Convert.ToInt32(pdate.Substring(0, 4));
                    int month = Convert.ToInt32(pdate.Substring(4, 2));


                    //    int day = Convert.ToInt32(pdate.Substring(6, 2));

                    //BenefitHelper.Data.Month.CompanyMonthSum gms = new BenefitHelper.Data.Month.CompanyMonthSum();
                    //gms = gms.GetCompanyMonthSum(month, year, db);
                    //ViewBag.gms = gms;

                    //BenefitHelper.Data.Month.BrokerageMonthTradeInfo info = new BenefitHelper.Data.Month.BrokerageMonthTradeInfo();
                    //var brokerageinfos = info.GetBrokerageMonthTradeInfo(year, month, db);
                    //ViewBag.brokerageinfos = brokerageinfos;

                    List<BenefitHelper.Data.Month.GroupMonthSum> groupmonthsums = new BenefitHelper.Data.Month.GroupMonthSum().GetGroupMonthSum(month, year, db);
                    ViewBag.groupmonthsums = groupmonthsums;

                    BenefitHelper.Data.Month.CompanyMonthSum change = new BenefitHelper.Data.Month.CompanyMonthSum();
                    change = change.GetCompanyMonthSum(month, year, db);//查询当月交易数据
                    ViewBag.change = change;//保存当月交易数据供页面调用

                    BenefitHelper.Data.Month.CompanyAvgMonth changemonth = new BenefitHelper.Data.Month.CompanyAvgMonth().GetCompanyAvgMonth(month, year, db);//当月均值
                    if (changemonth != null)
                    {
                        ViewBag.changemonth = changemonth;//保存当月均值数据供页面调用

                        BenefitHelper.Data.Year.CompanyYearAvg companyyearavg = new BenefitHelper.Data.Year.CompanyYearAvg().GetCompanyAvgYear(year, db);//查询小组均值
                        ViewBag.companyyearavg = companyyearavg;//保存年均值数据供页面调用


                        BenefitHelper.Data.Year.CompanyYearSum operatoryearsum = new BenefitHelper.Data.Year.CompanyYearSum().GetCompanyYearSum(year, db);//查询公司年累计
                        ViewBag.operatoryearsum = operatoryearsum;//保存公司年累计数据供页面调用

                        int yestermonth = 0;
                        int yesteryear = 0;
                        if (month == 1)
                        {
                            yestermonth = 12;
                            yesteryear = year - 1;
                        }
                        else
                        {
                            yestermonth = month - 1;
                            yesteryear = year;
                        }
                        BenefitHelper.Data.Month.CompanyMonthSum changemonth2 = new BenefitHelper.Data.Month.CompanyMonthSum().GetCompanyMonthSum(yestermonth, yesteryear, db);//去年同期值
                        ViewBag.changemonth2 = changemonth2;//去年同期值

                        js += BenefitHelper.Tools.ChartAtManager.DayShouyilv(month, year, db);
                        js += BenefitHelper.Tools.ChartAtManager.CompanyMonthBenefitString(month, year, db);
                    }
                    else
                    {
                        db.Dispose();
                        return Redirect("/Error/Index/" + uid + "/" + pdate);
                    }
                }
                else//查询年数据
                {
                    ViewBag.queryyear = pdate.Substring(0, 4);
                    ViewBag.querymonth = "";
                    //  ViewBag.queryday = pdate.Substring(6, 2);
                    ViewBag.pdate = pdate;
                    int year = Convert.ToInt32(pdate.Substring(0, 4));


                    List<BenefitHelper.Data.Year.GroupYearSum> groupmonthsums = new BenefitHelper.Data.Year.GroupYearSum().GetGroupYearSum(year, db);
                    ViewBag.groupmonthsums = groupmonthsums;

                    List<BenefitHelper.Data.Month.CompanyMonthSum> CompanyMonthSums = new BenefitHelper.Data.Month.CompanyMonthSum().GetCompanyMonthSum(year, db);
                    ViewBag.CompanyMonthSums = CompanyMonthSums;

                    js += BenefitHelper.Tools.ChartAtManager.MonthShouyilv(year, db);
                    js += BenefitHelper.Tools.ChartAtManager.CompanyYearBenefitString(year, db);
                }
            }
            else
            {
                db.Dispose();
                return Redirect("/Error/HasNoPrower/" + uid + "/" + pdate);
            }
            ViewBag.js = js;
            return View();
        }

    }
}
